<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_activity_theme}}".
 *
 * @property int $id
 * @property int $type_id
 * @property int $activity_id
 * @property string $name
 * @property string $format
 * @property string $value
 * @property string $label
 * @property string $input_type
 * @property string $setting
 * @property string $description
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class ShopActivityTheme extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const FORMAT_STRING = 'string';
    const FORMAT_INTEGER = 'integer';
    const FORMAT_BOOLEAN = 'boolean';
    const FORMAT_ATTACH = 'attach';
    const FORMAT_ARRAY = 'array';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_activity_theme}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type_id', 'activity_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name', 'format', 'value', 'label'], 'required'],
            [['value', 'setting'], 'string'],
            [['name', 'format', 'input_type'], 'string', 'max' => 64],
            [['label'], 'string', 'max' => 100],
            [['description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type_id' => 'Type ID',
            'activity_id' => 'Activity ID',
            'name' => 'Name',
            'format' => 'Format',
            'value' => 'Value',
            'label' => 'Label',
            'input_type' => 'Input Type',
            'setting' => 'Setting',
            'description' => 'Description',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getData() {
        if ($this->format == static::FORMAT_ATTACH) {
            return Url::attach($this->value);
        } elseif ($this->format == static::FORMAT_STRING) {
            return (string) $this->value;
        } elseif ($this->format == static::FORMAT_BOOLEAN) {
            return boolval($this->value);
        } elseif ($this->format == static::FORMAT_ARRAY) {
            if ($this->value) {
                return unserialize($this->value);
            }
            return [];
        }
        return $this->value;
    }

}
